﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class StoreDaySaleChartController : BaseController<usersorder>
    {
        //
        // GET: /StoreDaySaleChart/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }
        }

        StoreBll storeBll = new StoreBll();

        public class OrderModel
        {
            public string shopName;
            public string SaleMoney;
        }


        public ActionResult GetOrderAll2(string date1, string date2)
        {
            List<OrderModel> dataList = new List<OrderModel>();
            Expression<Func<usersorder, bool>> condition = null;
            //默认查询本月的  所有店铺的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2))
            {
                DateTime todayNow = DateTime.Now.Date;
                //本月 第一天
                DateTime now = DateTime.Now;
                DateTime beforeWeek = new DateTime(now.Year, now.Month, 1);
                TimeSpan ts = (TimeSpan)(todayNow - beforeWeek);
                int a=Convert.ToInt32( ts.TotalDays)+1;
                var query = bll.Search().ToList();
                var listOrder = query.Where(x => x.orderStatus == 1 && x.orderTime.Date <= now && x.orderTime.Date >= beforeWeek).GroupBy(x => x.storeid).Select(x => new { discountMoney = x.Sum(y => y.discountMoney), orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, }).ToList();
                foreach (var oneShop in listOrder)
                {
                    OrderModel orderModel = new OrderModel();
                    decimal? b=(oneShop.orderSumprice + oneShop.discountMoney)/a;
                    orderModel.SaleMoney = Math.Round(Convert.ToDecimal(b), 2).ToString();
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                    dataList.Add(orderModel);
                }
            }
            //按日期查询  
            if (date1 != null && date1 != "" && date2 != null && date2 != "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                TimeSpan ts = (TimeSpan)(datetime2.Date - datetime1.Date);
                int a = Convert.ToInt32(ts.TotalDays);
                condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var listOrder = bll.Search().Where(condition).GroupBy(x => x.storeid).Select(x => new { discountMoney = x.Sum(y => y.discountMoney), x.FirstOrDefault().orderCode, orderSumprice = x.Sum(y => y.orderSumprice), x.FirstOrDefault().storeid, }).ToList();
                foreach (var oneShop in listOrder)
                {
                    OrderModel orderModel = new OrderModel();
                    decimal? b = (oneShop.orderSumprice + oneShop.discountMoney) / a;
                    orderModel.SaleMoney = Math.Round(Convert.ToDecimal(b), 2).ToString();
                    orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                    dataList.Add(orderModel);//  
                }
            }
            return Json(dataList, JsonRequestBehavior.AllowGet);
        }


    }
}
